<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  @active_tab = 'settings'
  css_bundle :statistics
  add_crumb t('#crumbs.stats', "Statistics")
 %>

<% content_for :page_title, join_title(t(:page_title, "Course Statistics"), @context.name) %>
<div style="padding: 0 20px;" id="stats">
<h1><%= t 'headings.stats', 'Statistics for %{course}', :course => @context.name %></h1>

<% active_students = @context.participating_students.where("enrollments.type<>'StudentViewEnrollment'").enrolled_before(@range_end).count %>
<% inactive_students = @context.student_enrollments.before(@range_end).select{|e| e.invited? }.length %>

<% active_topics =  @context.discussion_topics.active.before(@range_end).pluck(:id) %>
<% total_topics = active_topics.length %>
<% total_entries = DiscussionEntry.active.where(:discussion_topic_id => active_topics).count %>
<% new_topics = @context.discussion_topics.active.before(@range_end).select{|t| t.created_at > @range_start}.length %>
<% new_entries = DiscussionEntry.active.after(@range_start).where(:discussion_topic_id => active_topics).count %>
<% new_assignments = @context.assignments.active.before(@range_end).select{|a| a.expects_submission? && a.created_at > @range_start }.length %>
<% new_submissions = @context.submissions.before(@range_end).having_submission.where('submitted_at > ?', @range_start).count %>
<% total_assignments = @context.assignments.active.before(@range_end).select{|a| a.expects_submission? }.length %>
<% total_submissions = @context.submissions.before(@range_end).having_submission.count %>

<%
  rubric_associations = @context.rubric_associations.bookmarked.before(@range_end).distinct.pluck(:rubric_id)
%>
<div id="reports-tabs" style="display:none;">
  <ul>
    <li><a href="#tab-totals">{{#t}}Totals{{/t}}</a></li>
    <li><a href="#tab-assignments">{{#t}}Assignments{{/t}}</a></li>
    <li><a href="#tab-students" id="students_stats_tab">{{#t}}Students{{/t}}</a></li>
    <li><a href="#tab-files">{{#t}}File Storage{{/t}}</a></li>
  </ul>
  <div class="stats" id="tab-totals">
    <h2>{{#t}}Running Totals{{/t}}</h2>
    <table>
      <tr>
        <td>{{#t}}Discussions{{/t}}</td>
        <td><%= total_topics %></td>
      </tr><tr>
        <td class="indent">{{#t}}Discussion Posts{{/t}}</td>
        <td class="indent"><%= total_entries %></td>
      </tr><tr>
        <td class="indent">{{#t}}New Discussions{{/t}}</td>
        <td class="indent"><%= new_topics %></td>
      </tr><tr>
        <td class="indent">{{#t}}New Discussion Entries{{/t}}</td>
        <td class="indent"><%= new_entries %></td>
          </tr><tr>
        <td>{{#t}}Assignments{{/t}}</td>
        <td><%= @context.assignments.active.before(@range_end).count %></td>
      </tr><tr>
        <td class="indent">{{#t}}Assignment Groups{{/t}}</td>
        <td class="indent"><%= @context.assignment_groups.active.before(@range_end).count %></td>
      </tr><tr>
        <td class="indent">{{#t}}Course Rubrics{{/t}}</td>
        <td class="indent"><%= rubric_associations.count %></td>
      </tr><tr>
        <td class="indent">{{#t}}All Rubrics{{/t}}</td>
        <td class="indent"><%= @context.rubrics.active.count %></td>
      </tr><tr>
        <td>{{#t}}Active Students{{/t}}</td>
        <td><%= active_students %></td>
      </tr><tr>
        <td class="indent">{{#t}}Unaccepted Students{{/t}}</td>
        <td class="indent"><%= inactive_students %></td>
      </tr><tr>
        <td>{{#t}}Quizzes{{/t}}</td>
        <td class="quiz_count"><%= @context.quizzes.active.before(@range_end).count %></td>
      </tr><tr>
        <td class="indent">{{#t}}Quiz Questions{{/t}}</td>
        <td class="indent quiz_question_count"><%= @context.quizzes.active.before(@range_end).map{|q| (q.quiz_data || []).length}.sum %></td>
      </tr><tr>
        <td class="indent">{{#t}}Quiz Submissions{{/t}}</td>
        <td class="indent quiz_submission_count"><%= Quizzes::QuizSubmission.where(quiz_id: @context.quizzes.active.before(@range_end)).count %></td>
      </tr>
    </table>
  </div>
  <div class="stats" id="tab-assignments">
    <h2>{{#t}}Assignments Usage Report{{/t}}</h2>
    <table>
      <thead><tr>
        <th>{{#t}}Submission Type{{/t}}</th>
        <th>{{#t}}# Assignments{{/t}}</th>
        <th>{{#t}}# Submissions{{/t}}</th>
      </tr>
      </thead>
      <tfoot>
      <tr>
        <th>{{#t}}New{{/t}}</th>
        <th><%= new_assignments %></th>
        <th><%= new_submissions %></th>
      </tr><tr>
        <th>{{#t}}Any{{/t}}</th>
        <th><%= total_assignments %></th>
        <th><%= total_submissions %></th>
      </tr></tfoot>
      <tbody>
      <tr>
        <td>{{#t}}File Upload{{/t}}</td>
        <td><%= @context.assignments.active.before(@range_end).select{|a| a.submission_types.match(/online_upload/) }.length %></td>
        <td><%= @context.submissions.before(@range_end).where(:submission_type => 'online_upload').count %></td>
      </tr><tr>
        <td>{{#t}}Text Entry{{/t}}</td>
        <td><%= @context.assignments.active.before(@range_end).select{|a| a.submission_types.match(/online_text_entry/) }.length %></td>
        <td><%= @context.submissions.before(@range_end).where(:submission_type => 'online_text_entry').count %></td>
      </tr><tr>
        <td>{{#t}}Website URL{{/t}}</td>
        <td><%= @context.assignments.active.before(@range_end).select{|a| a.submission_types.match(/online_url/) }.length %></td>
        <td><%= @context.submissions.before(@range_end).where(:submission_type => 'online_url').count %></td>
      </tr><tr>
        <td>{{#t}}Quiz{{/t}}</td>
        <td><%= @context.assignments.active.before(@range_end).select{|a| a.submission_types.match(/online_quiz/) }.length %></td>
        <td><%= @context.submissions.before(@range_end).where(:submission_type => 'online_quiz').count %></td>
      </tr><tr>
        <td>{{#t}}Discussion Topic{{/t}}</td>
        <td><%= @context.assignments.active.before(@range_end).select{|a| a.submission_types.match(/discussion_topic/) }.length %></td>
        <td><%= @context.submissions.before(@range_end).where(:submission_type => 'discussion_topic').count %></td>
      </tr>
      </tbody>
    </table>
  </div>
  <div id="tab-students">
    <h2>{{#t}}Recently Logged-In Users{{/t}}</h2>
    <ul class="item_list"></ul>
  </div>
  <div class="stats" id="tab-files">
    <h2>{{#t}}File Storage{{/t}}</h2>
    <table>
      <tr>
        <td><%= before_label('allotted_storage', %{Allotted Storage}) %></td>
        <td style="padding-left: 10px; text-align: right;"><%= number_to_human_size(@context.quota) %></td>
        <td></td>
      </tr>
      <tr>
        <td><%= before_label('uploaded_files', %{Uploaded Files}) %></td>
        <td style="padding-left: 10px; text-align: right;"><%= number_to_human_size(@files_size) %></td>
        <td style="padding-left: 10px; text-align: right;"><%= t 'file_count', 'file', :count => @file_count %></td>
      </tr>
      <tr>
        <td><%= before_label('media_files', %{Media Files}) %></td>
        <td style="padding-left: 10px; text-align: right;"><%= number_to_human_size(@media_files_size) %></td>
        <td style="padding-left: 10px; text-align: right;"><%= t 'file_count', 'file', :count => @media_file_count %></td>
      </tr>
    </table>
  </div>
</div>
</div>
<% js_bundle :course_statistics %>
